package com.songlanyun.modules.apphome.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.songlanyun.common.enums.CommentConstant;
import com.songlanyun.common.enums.OpusConstant;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.modules.apphome.model.vo.HomePageVO;
import com.songlanyun.modules.apphome.service.HomePageService;
import com.songlanyun.modules.article.entity.Article;
import com.songlanyun.modules.article.service.ArticleService;
import com.songlanyun.modules.browse.service.BrowseRecordService;
import com.songlanyun.modules.collect.service.UserCollectGoodsService;
import com.songlanyun.modules.comment.service.OrderGoodsCommentService;
import com.songlanyun.modules.comment.service.UserCommentService;
import com.songlanyun.modules.concerns.model.ConcernStatisticsVo;
import com.songlanyun.modules.concerns.service.UserConcernService;
import com.songlanyun.modules.like.model.vo.MyLikeVO;
import com.songlanyun.modules.like.service.LikeRecordService;
import com.songlanyun.modules.opus.entity.Opus;
import com.songlanyun.modules.opus.service.OpusService;
import com.songlanyun.modules.share.service.ShareRecordService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

@Service("homePageServiceImpl")
public class HomePageServiceImpl implements HomePageService {

    @Resource
    private BrowseRecordService browseRecordService;
    @Resource
    private LikeRecordService likeRecordService;
    @Resource
    private ShareRecordService shareRecordService;
    @Resource
    private UserCommentService userCommentService;
    @Resource
    private OrderGoodsCommentService orderGoodsCommentService;
    @Resource
    private OpusService opusService;
    @Resource
    private ArticleService articleService;
    @Resource
    private UserConcernService userConcernService;

    @Resource
    private UserCollectGoodsService userCollectGoodsService;

    @Resource
    private ThreadPoolExecutor threadPoolExecutor;


    @Override
    public HomePageVO myStatistics(Long userId) {
        HomePageVO vo = new HomePageVO();

        CompletableFuture<HomePageVO> releaseNumFuture = CompletableFuture.supplyAsync(() -> {
            //我的发布总数 -- 包括作品
            Integer releaseNum = opusService.releaseNum(userId);
            vo.setReleaseNum(releaseNum);
            return vo;
        }, threadPoolExecutor);

        CompletableFuture<HomePageVO> concernFansNumFuture = CompletableFuture.supplyAsync(() -> {
            //我的关注总数 和粉丝总数
            ConcernStatisticsVo concernStatisticsVo = userConcernService.statisticsPerson(userId);
            vo.setConcernNum(concernStatisticsVo.getConcernNum());
            vo.setFansNum(concernStatisticsVo.getFansNum());
            return vo;
        }, threadPoolExecutor);

        CompletableFuture<HomePageVO> likeSumFuture = CompletableFuture.supplyAsync(() -> {
            //我的点赞总数
            Integer likeSum = likeRecordService.likeSumByUserId(userId);
            vo.setLikeSum(likeSum);
            return vo;
        }, threadPoolExecutor);

        CompletableFuture<HomePageVO> commentSumFuture = CompletableFuture.supplyAsync(() -> {
            //我的评论总数 -- 包括作品、商品评论
            Integer commentSum = userCommentService.commentSum(userId);
            vo.setCommentSum(commentSum);
            return vo;
        }, threadPoolExecutor);

        CompletableFuture<HomePageVO> collectSumFuture = CompletableFuture.supplyAsync(() -> {
            //我的收藏总数
            Integer collectSum = userCollectGoodsService.collectSum(userId);
            vo.setCollectSum(collectSum);
            return vo;
        }, threadPoolExecutor);

        try {
            CompletableFuture.allOf(
                    releaseNumFuture,
                    concernFansNumFuture,
                    likeSumFuture,
                    commentSumFuture,
                    collectSumFuture
            ).get();
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }

        return vo;

    }


    @Override
    public PageUtils myList(Long userId) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("likeUserId", userId);

        IPage<MyLikeVO> page = likeRecordService.myLikePage(params);
        page.getRecords().forEach(like -> {
            OpusConstant.ModelType modelType = like.getModelType();
            //设置点赞数量
            switch (modelType) {
                case OPUS:
                    Opus opus = opusService.getById(like.getModelId());
                    if (opus != null) {
                        like.setLikeNum(opus.getLikeNum());
                    }
                    break;
                case ARTICLE:
                    Article article = articleService.getById(like.getModelId());
                    if (article != null) {
                        like.setLikeNum(article.getLikeNum());
                    }
                    break;
            }
        });

        return new PageUtils(page);


    }

    @Override
    public void delRecord(OpusConstant.ModelType modelType, Long id) {
        //浏览
        browseRecordService.modelDel(modelType, id);
        //点赞
        likeRecordService.modelDel(modelType, id);
        //分享
        shareRecordService.modelDel(modelType, id);
        //评论
        switch (modelType) {
            case OPUS:
                userCommentService.modelDel(CommentConstant.CommentModelType.OPUS, id);
                break;
            case ARTICLE:
                userCommentService.modelDel(CommentConstant.CommentModelType.ARTICLE, id);
                break;
        }


    }
}
